package Rangel.ambulance.search;


import adf.agent.info.AgentInfo;
import adf.agent.info.WorldInfo;
import Rangel.common.world.helper.RangelWorldHelper;
import Rangel.common.world.entity.RangelBuilding;
import Rangel.common.world.entity.RangelRoad;
import Rangel.common.world.entity.Path;
import rescuecore2.standard.entities.Road;

import java.util.*;


public class SimpleSearchDecisionMaker {
    private boolean searchInPartition = false;
    private List<Path> shouldDiscoverPaths;
    private List<Path> discoveredPaths;

    private Path pathInProgress;
    private RangelWorldHelper world;

    protected Set<Path> validPaths;


    public SimpleSearchDecisionMaker(RangelWorldHelper world, AgentInfo ai, WorldInfo wi) {
        this.world = world;
        validPaths = new HashSet<>();
    }


    public void update() {
        if (!searchInPartition) {
            validPaths.addAll(world.getPaths());
        }
        setShouldDiscoverPaths();
    }

    public void initialize() {
        discoveredPaths = new ArrayList<>();

        shouldDiscoverPaths = new ArrayList<>();

        pathInProgress = null;
    }

    public Path getNextPath() {
        Path nextPath = null;
        if (pathInProgress == null) {
            if (!shouldDiscoverPaths.isEmpty()) {
                nextPath = getMyPath();
                if (!shouldDiscoverPaths.contains(nextPath)) {
                    nextPath = shouldDiscoverPaths.get(0);
                }
            } else {
                discoveredPaths.clear();
                setShouldDiscoverPaths();
            }
        } else {
            Set<Path> neighbours = pathInProgress.getNeighbours();
            for (Path path : neighbours) {
                if (shouldDiscoverPaths.contains(path)) {
                    nextPath = path;
                    break;
                }
            }
            if (nextPath == null) {
                if (shouldDiscoverPaths.isEmpty()) {
                    discoveredPaths.clear();
                    setShouldDiscoverPaths();
                } else {
                    int size = shouldDiscoverPaths.size();
                    Random random = new Random(System.currentTimeMillis());
                    int index = Math.abs(random.nextInt()) % size;
                    nextPath = shouldDiscoverPaths.get(index);
                }
            }
        }
        if (nextPath != null) {
            shouldDiscoverPaths.remove(nextPath);
            if (!discoveredPaths.contains(nextPath))
                discoveredPaths.add(nextPath);
        }
        pathInProgress = nextPath;
        return pathInProgress;
    }


    private void setShouldDiscoverPaths() {
        shouldDiscoverPaths.clear();
        if (searchInPartition) {
            shouldDiscoverPaths.addAll(validPaths);
        } else {
            shouldDiscoverPaths.addAll(world.getPaths());
        }
        shouldDiscoverPaths.removeAll(discoveredPaths);
    }

    private Path getMyPath() {
        Path myPath;
        if (world.getSelfPosition() instanceof Road) {
            RangelRoad RangelRoad = world.getRangelRoad(world.getSelfPosition().getID());
            myPath = RangelRoad.getPaths().get(0);
        } else {
            RangelBuilding RangelBuilding = world.getRangelBuilding(world.getSelfPosition().getID());
            Road roadEntrance = RangelBuilding.getEntrances().get(0).getNeighbour();
            RangelRoad RangelRoad = world.getRangelRoad(roadEntrance.getID());
            myPath = RangelRoad.getPaths().get(0);
        }
        return myPath;
    }
}
